﻿Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports DevComponents.DotNetBar

Public Class frmPersonas

    Dim variableconexion1 As New SqlConnection(My.Settings.CITRA_ERPConnectionString)
    Dim variableconexion2 As New SqlConnection(My.Settings.ctCITRAAUSL2012ConnectionString)


    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

        If ModGeneral.EstadoRed() = False Then
            StatusPersona.Visible = True
        Else
            StatusPersona.Visible = False
        End If

    End Sub

    Private Sub btnAtrasPersonas_Click(sender As Object, e As EventArgs) Handles btnAtrasPersonas.Click
        Me.Close()
    End Sub

    Public Sub llenarCombo()

        Dim ds As New DataSet
        Dim da As New SqlDataAdapter("select idEmpleado, NombreCompleto from empleados Order by NombreCompleto;" & "select idEmpleado, NombreCompleto from empleados Order by NombreCompleto;", variableconexion2)
        da.Fill(ds)

        cmbEmpleado.DataSource = ds.Tables(0)
        cmbEmpleado.DisplayMember = "NombreCompleto"
        cmbEmpleado.ValueMember = "idEmpleado"

        cmbSuperior.DataSource = ds.Tables(1)
        cmbSuperior.DisplayMember = "NombreCompleto"
        cmbSuperior.ValueMember = "idEmpleado"

    End Sub

    Public Sub llenarTablaPersonal()

        Dim DS As New DataSet
        Dim DA As New SqlDataAdapter("SELECT p.idPersonal,e.NombreCompleto as Empleado,e.idEmpleado,e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior ,p.Funcion FROM CITRA_ERP.dbo.Personal p  left join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado left join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado order by idpersonal", variableconexion1)
        DA.Fill(DS)

        dgwPersonal.DataSource = DS.Tables(0)

        dgwPersonal.Columns(2).Visible = False
        dgwPersonal.Columns(4).Visible = False


    End Sub

    Private Sub frmPersonas_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        llenarCombo()
        llenarTablaPersonal()
        LblValoresAnterioresEditar.BackColor = Color.PaleVioletRed
        dgwPersonal.Columns(1).Width = 400
        dgwPersonal.Columns(3).Width = 400


    End Sub

    Private Sub ButtonItem7_Click(sender As Object, e As EventArgs) Handles btnAceptar.Click

        insertarActualizar()

    End Sub

    Public Sub insertarActualizar()

        Dim status As String

        status = "si"

        'Checamos que los campos esten completos para insercion de los valores

        If cmbEmpleado.SelectedValue.ToString.Equals("") Then

            status = "no"


        ElseIf cmbSuperior.SelectedValue.ToString.Equals("") Then

            status = "no"

        ElseIf cmbFuncion.Text.ToString.Equals("Función") Then

            status = "no"

        End If


        'Si los campos fueron llenados correctamente se procede a realizar lo siguiente comparamos si se quiere modificar o insertar valores 

        If status.Equals("si") Then

            If lblModificarPersonal.Text.Equals("") Then

                Dim Query As New DataSet_Prod_ServTableAdapters.PersonalTableAdapter
                'Insertamos valores
                Query.Insert(cmbEmpleado.SelectedValue.ToString, cmbSuperior.SelectedValue.ToString, cmbFuncion.Text.ToString)
                PageSlider1.SelectedPage = PageSliderPage2
                llenarTablaPersonal()
                ToastNotification.Show(Me, "Insertado correctamente", My.Resources.checkHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

            Else

                Dim DS As New DataSet
                Dim DA As New SqlDataAdapter("UPDATE Personal SET " & _
                                             "idEmpleado = " & cmbEmpleado.SelectedValue.ToString & "," & _
                                             "idSuperior = " & cmbSuperior.SelectedValue.ToString & "," & _
                                             "Funcion = " & cmbFuncion.Text.ToString & _
                                             "WHERE IDPERSONAL = " & lblModificarPersonal.Text & "", variableconexion1)
                DA.Fill(DS)
                PageSlider1.SelectedPage = PageSliderPage2
                llenarTablaPersonal()
                ToastNotification.Show(Me, "Modificado correctamente", My.Resources.checkHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

            End If

        Else

            ToastNotification.Show(Me, "Completa todos los campos", My.Resources.cancelHommer, 3000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)

        End If

    End Sub

    Private Sub ButtonItem8_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click

        limpiarCampos()
        llenarCombo()


    End Sub

    Public Sub Modificar()

        Dim fila As Integer
        fila = dgwPersonal.CurrentCell.RowIndex
        Dim valor As String
        valor = dgwPersonal.Rows(fila).Cells(0).Value.ToString()

        lblModificarPersonal.Text = dgwPersonal.Rows(fila).Cells(0).Value.ToString()
        LblValoresAnterioresEditar.Visible = True
        lblEtiquetaEmpleado.Visible = True
        lblEtiquetaSuperior.Visible = True
        lblEtiquetaFuncion.Visible = True
        lblMostrarNombreEmpleado.Visible = True
        lblMostrarSuperior.Visible = True
        lblMostrarFuncion.Visible = True

        lblMostrarNombreEmpleado.Text = dgwPersonal.Rows(fila).Cells(1).Value.ToString()
        lblMostrarSuperior.Text = dgwPersonal.Rows(fila).Cells(3).Value.ToString()
        lblMostrarFuncion.Text = dgwPersonal.Rows(fila).Cells(5).Value.ToString()

        cmbFuncion.Text = dgwPersonal.Rows(fila).Cells(5).Value.ToString()

        cmbEmpleado.SelectedValue = dgwPersonal.Rows(fila).Cells(2).Value.ToString()

        cmbSuperior.SelectedValue = dgwPersonal.Rows(fila).Cells(4).Value.ToString()

        PageSlider1.SelectedPage = PageSliderPage1

        ToastNotification.Show(Me, "Elige ahora los nuevos valores", My.Resources.ihommer, 5000, eToastGlowColor.Blue, eToastPosition.MiddleCenter)



    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click

        'Este es el boton del slide de la tabla para modificar los datos
        Modificar()
        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        'Este es el boton del slide de la tabla para mostrar los primeros datos
        PageSlider1.SelectedPage = PageSliderPage1
        limpiarCampos()
        llenarCombo()
        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Public Sub Buscar()

        Dim DS As New DataSet
        Dim DA As New SqlDataAdapter("SELECT p.idPersonal, e.NombreCompleto as Empleado, e.IdEmpleado, e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior, p.Funcion FROM CITRA_ERP.dbo.Personal p join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado and e2.NombreCompleto like  '%" & txtbuscar.Text.ToString & "%' union SELECT p.idPersonal, e.NombreCompleto as Empleado,e.IdEmpleado,  e2.NombreCompleto as Superior,e2.IdEmpleado as idSuperior, p.Funcion FROM CITRA_ERP.dbo.Personal p join  ctCITRAAUSL2012.dbo.empleados e on p.IDEMPLEADO  = e.IdEmpleado  and e.NombreCompleto like  '%" & txtbuscar.Text.ToString & "%' join ctCITRAAUSL2012.dbo.empleados e2 on p.IDSUPERIOR=e2.idempleado order by idpersonal", variableconexion1)
        DA.Fill(DS)
        dgwPersonal.DataSource = DS.Tables(0)
        dgwPersonal.Columns(2).Visible = False
        dgwPersonal.Columns(4).Visible = False

    End Sub

    Private Sub txtbuscar_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtbuscar.KeyPress
        Buscar()
        btncancelarbusqueda.Visible = True
    End Sub

    Private Sub btncancelarbusqueda_Click(sender As Object, e As EventArgs) Handles btncancelarbusqueda.Click

        llenarTablaPersonal()
        btncancelarbusqueda.Visible = False
        txtbuscar.Text = ""

    End Sub

    Public Sub limpiarCampos()

        cmbFuncion.Text = "Función"

        lblModificarPersonal.Text = ""

        LblValoresAnterioresEditar.Visible = False
        lblEtiquetaEmpleado.Visible = False
        lblEtiquetaSuperior.Visible = False
        lblEtiquetaFuncion.Visible = False
        lblMostrarNombreEmpleado.Visible = False
        lblMostrarSuperior.Visible = False
        lblMostrarFuncion.Visible = False

        lblMostrarNombreEmpleado.Text = ""
        lblMostrarSuperior.Text = ""
        lblMostrarFuncion.Text = ""

    End Sub


End Class